// ////////////////////////////////////////////////////////////////////////////////////////
// NAME: BatteryScanner.java 
// AUTHOR: CDT Riley Mckenna 
// LAST MODIFIED: 28 March 2012 
// DESCRIPTION: Contains code for output of Battery Scanner
// LIMITATIONS: 
// CITATIONS: In code
// 
// ////////////////////////////////////////////////////////////////////////////////////////

package prototype.revision;

import java.io.File;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BatteryScanner extends GenericActivity {
	private File f = null;
	private TextView batteryLevel;
	private TextView batteryCurrent;
	private TextView batteryAvg;
	private TextView batteryTemp;
	private TextView batteryVolts;
	private Button stop;
	private CommandLine cmd = new CommandLine();
	private String batStats = null;
	private String current_now = "/sys/class/power_supply/battery/current_now";
	private String voltage_now = "/sys/class/power_supply/battery/voltage_now";
	private String current_avg = "/sys/class/power_supply/battery/current_avg";
	private String temp = "/sys/class/power_supply/battery/temp";
	private final Handler my_handler = new Handler();
	private int my_sample_poll = 22000;
	private FileOperations fo = new FileOperations(this);
	private final String filename = "batterystats";
	private int batLevel;
	private boolean running = true;
	private final String txtStop = "Stop";
	private final String txtStart = "Start";

	public BatteryScanner() {

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.batterystats);
		batteryLevel = (TextView) findViewById(R.id.batteryLevel);
		batteryCurrent = (TextView) findViewById(R.id.batteryCurrent);
		batteryAvg = (TextView) findViewById(R.id.batteryCurrentAvg);
		batteryTemp = (TextView) findViewById(R.id.batteryTemp);
		batteryVolts = (TextView) findViewById(R.id.batteryVolts);
		registerReceiver(batteryReceiver, new IntentFilter(
				Intent.ACTION_BATTERY_CHANGED));
		stop = (Button) findViewById(R.id.stop);
		my_handler.postDelayed(mUpdateUITimerTask, my_sample_poll);
		fo.ClearFile(filename);
		fo.CreateFile(filename);
		stop.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				action();
			}
		});
	}

	public void action() {
		// TODO Auto-generated method stub
		if (running == false) {
			running = true;
			stop.setText((CharSequence) txtStop);
		} else if (running == true) {
			running = false;
			stop.setText(txtStart);
		}
	}

	public void updateBatteryCondition(int level) {
		batteryLevel.setText(level + "%");
		getCurrent();
		getVolts();
		this.getCurrentAvg();
		this.getTemp();
	}

	public void updateBatteryConditionToFile(int level) {
		sendBatteryStatToFile(current_avg, "amps");
		sendBatteryStatToFile(current_now, "amps");
		sendBatteryStatToFile(voltage_now, "v");
		sendBatteryStatToFile(temp, "C");
	}

	private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			batLevel = intent.getIntExtra("level", 0);
			updateBatteryCondition(batLevel);
			updateBatteryConditionToFile(batLevel);
		}
	};

	public void getCurrent() {
		this.getBatteryStat(this.current_now, " amps", batteryCurrent);
	}

	public void getVolts() {
		this.getBatteryStat(this.voltage_now, " mV", batteryVolts);
	}

	public void getTemp() {
		this.getBatteryStat(this.temp, "C", batteryTemp);
	}

	public void getCurrentAvg() {
		this.getBatteryStat(this.current_avg, " amps", batteryAvg);
	}

	public void getBatteryStat(String path, String units, TextView textView) {
		String new_stats = "";
		int constant = 2;
		if (units == "C") {
			constant = 1;
		}
		batStats = cmd.catFile(path).trim();
		for (int i = 0; i < batStats.length(); i++) {
			if (i == (batStats.length() - constant)) {
				new_stats = new_stats + '.' + batStats.charAt(i);
			} else {
				new_stats = new_stats + batStats.charAt(i);
			}
		}
		textView.setText(new_stats + units);

	}

	public void sendBatteryStatToFile(String path, String units) {
		String new_stats = "";
		int constant = 2;
		if (units == "C") {
			constant = 1;
		}
		batStats = cmd.catFile(path).trim();
		for (int i = 0; i < batStats.length(); i++) {
			if (i == (batStats.length() - constant)) {
				new_stats = new_stats + '.' + batStats.charAt(i);
			} else {
				new_stats = new_stats + batStats.charAt(i);
			}
		}
		if (units == "C") {
			fo.AppendSentenceToFile(new_stats + '\n', filename);
		} else {
			fo.AppendSentenceToFile(new_stats + ", ", filename);
		}
	}

	public void refresh() {
		getCurrent();
		getVolts();
		this.getCurrentAvg();
		this.getTemp();
		updateBatteryConditionToFile(batLevel);
	}

	public File getFile() {
		return f;
	}

	private Runnable mUpdateUITimerTask = new Runnable() {
		public void run() {
			if (running) {
				refresh();
				Log.i("Runnable", "PackageName" + "Refreshing.");
				my_handler.postDelayed(mUpdateUITimerTask, my_sample_poll);
			} else {

			}

		}

	};

}
